import { get, lsGetStr, post } from "../../utils/request.js";
var { getLoginedUser, fnLoading, fnHideLoading, fnSuccess, fnFail } = require('../../utils/index')
Component({
  properties: {
    redirectTo:{
      type: String,
    },
    verifyPhone: {
      type: String,
      observer: function (newVal, oldVal, changedPath) {
        //更新数据
        console.log('数据变化')
        if (newVal=='true'){
          this.setData({
            couponsBg:true,
          })
        }

      }

    } // 简化的定义方式
  },
 
  data:{
    currentTime: 61,
    text: "获取验证码",
    couponsBg: false,
    loginBgFlag: false,
  },
  lifetimes: {
    attached: function () { },
    moved: function () { },
    detached: function () { },
  },
  attached() {}, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () {},
  methods: {
    passWdInput: function (e) {
      this.setData({
        phone: e.detail.value
      })
    },
    obtain: function () {
      let that = this;
      that.setData({
        disabled: true,
        color: "#E8E8E8"
      })
      var phone = this.data.phone || '';
      var currentTime = this.data.currentTime;
      var warn = null;
      if (phone == '') {
        warn = "号码不能为空";
      } else if (phone.trim().length != 11 || !/^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/.test(phone)) {
        warn = "手机号格式不正确";
      } else {
        get({
          path: '/phone-code?phone=' + phone,
        })
          .then(function (res) {
            console.log(res)
            fnSuccess("发送成功~")
          })
          .catch(function () {
            fnFail("发送失败，请稍后重试~")
          })
      }
      if (warn == null) {
        var interval = setInterval(function () {
          currentTime--;
          that.setData({
            text: currentTime + 's',
          })
          if (currentTime <= 0) {
            clearInterval(interval);
            that.setData({
              text: '重新发送',
              currentTime: 61,
              disabled: false,
              color: '#E8E8E8'
            })
          }
        }, 1000);
      }
      if (warn != null) {
        fnFail(warn);
        that.setData({
          disabled: false,
          color: '#E8E8E8'
        })
        return;
      }
    },
    addCode: function (e) {
      this.setData({
        code: e.detail.value
      })
    },
    onSubmit: function () {
      var phone = this.data.phone;
      var code = this.data.code;
      var that = this;
      if (!code) {
        return fnFail('请输入短信验证码');
      }
      getLoginedUser()
        .then(function (user) {
          return post({
            path: '/user/verify-phone',
            data: {
              skey: lsGetStr('skey'),
              id: user.id,
              phone: phone,
              code: code
            }
          })
        })
        .then(function (res) {
          fnSuccess('验证成功~')
          that.setData({
            couponsBg: false
          })
          if (that.properties.redirectTo){
            wx.navigateTo({
              url: that.properties.redirectTo,
            })
          }else{
           // that.onLoad(that.data.options || {});
          }
          
        })
        .catch(function (e) {
          fnFail(e || "发送失败")
        })
    },
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      let that = this; 
      getLoginedUser()
      .then(function(user){
        if (user && !user.phone){
          that.setData({
            loginBgFlag:false,
            couponsBg:false
          })
        }
      })
      .catch(function(e){
        that.setData({
          loginBgFlag: true,
        })
      })
    },
  },
})